--[[
    牌型计算

--]]


local M = class("ccardlogic")



function M:getVal(a)
    return a & 0x0F
end
function M:getColor(a)
    return (a & 0xF0)/16 + 1
end

function M:findCardType(kind, cardData)
    local data = nil
    if kind < 0 or kind > 10 then return data end
    if kind == 1 then
        data = self:findGaopai(cardData)
    elseif kind == 2 then
        data = self:findDuizi(cardData)
    elseif kind == 3 then
        data = self:findLiangdui(cardData)
    elseif kind == 4 then
        data = self:findSanzhang(cardData)
    elseif kind == 5 then
        data = self:findShunzi(cardData)
    elseif kind == 6 then
        data = self:findTonghua(cardData)
    elseif kind == 7 then
        data = self:findHulu(cardData)
    elseif kind == 8 then
        data = self:findSitiao(cardData)
    elseif kind == 9 then
        data = self:findTonghuashun(cardData)
    elseif kind == 10 then
        data = self:findHuangjia(cardData)
    end
    if not data or #data ~= 5 then return nil end
    return data
end

function M:findGaopai(cardData)
    self:sortByValues(cardData)

    if #cardData == 7 then
        table.remove(cardData, 6)
        table.remove(cardData, 7)
    end

    return cardData

end

function M:findDuizi(cardData)
    self:sortByValues(cardData)
    local retData = self:searchSameValue(cardData, 2)
    if #retData <= 0 then return nil end
    local value = self:getVal(retData[1])
    for _, v in ipairs(cardData) do
        if self:getVal(v) ~= value then
            table.insert(retData, v)
        end
        if #retData == 5 then break end
    end
    return retData
end

function M:findLiangdui(cardData)
    local data = {}
    self:sortByValues(cardData)
    local retData, index = {}, nil
    local counts = {2, 2}
    for _, count in ipairs(counts) do
        retData = self:searchSameValue(cardData, count)
        if #retData <= 0 then return nil end

        for k, v in ipairs(retData) do
            table.insert(data, v)
            index = table.indexof(cardData, v)
            if index then table.remove(cardData, index) end
        end
    end
    table.insert(data, cardData[1])
    return data
end

function M:findSanzhang(cardData)
    self:sortByValues(cardData)
    local retData = self:searchSameValue(cardData, 3)
    if #retData <= 0 then return nil end
    local value = self:getVal(retData[1])
    for _, v in ipairs(cardData) do
        if self:getVal(v) ~= value then
            table.insert(retData, v)
        end
        if #retData == 5 then break end
    end
    return retData
end

function M:findShunzi(cardData)
    self:sortByValues(cardData)
    local temp = {}
    local card = cardData[1]
    table.insert(temp, card)
    for i = 2, #cardData do
        if self:getVal(card) - self:getVal(cardData[i]) == 1 then
            table.insert(temp, cardData[i])
            if #temp == 5 then break end
        else
            temp = {}
            table.insert(temp, cardData[i])
        end
        card = cardData[i]
    end
    return temp
end


function M:findTonghua(cardData)
    local data = {}
    self:sortByValues(cardData)
    local temp, color = {}, nil
    for _, v in ipairs(cardData) do
        color = self:getColor(v)
        if not temp[color] then temp[color] = {} end
        if #temp[color] < 5 then
            table.insert(temp[color], v)
        end
    end
    for _, v in pairs(temp) do
        if #v == 5 then
            data = v
            break
        end
    end
    return data
end


function M:findHulu(cardData)
    local data = {}
    self:sortByValues(cardData)
    local retData, index = {}, nil
    local counts = {3, 2}
    for _, count in ipairs(counts) do
        retData = self:searchSameValue(cardData, count)
        if #retData <= 0 then return nil end
        for k, v in ipairs(retData) do
            table.insert(data, v)
            index = table.indexof(cardData, v)
            if index then table.remove(cardData, index) end
        end
    end
    return data
end

function M:findSitiao(cardData)
    self:sortByValues(cardData)
    local retData = self:searchSameValue(cardData, 4)
    if #retData <= 0 then return nil end
    --print("findSitiao:", tostring(cardData), tostring(retData))
    local value = self:getVal(retData[1])
    for _, v in ipairs(cardData) do
        if self:getVal(v) ~= value then
            table.insert(retData, v)
        end
        if #retData == 5 then break end
    end
    return retData
end

function M:findTonghuashun(cardData)
    local data = nil
    self:sortByValues(cardData)
    local temp, color = {}, nil
    for _, v in ipairs(cardData) do
        color = self:getColor(v)
        if not temp[color] then temp[color] = {} end
        table.insert(temp[color], v)
    end
    for _, v in pairs(temp) do
        if #v >= 5 then
            data = self:findShunzi(v)
            if data then break end
        end
    end
    return data
end

function M:findHuangjia(cardData)
    local data = self:findTonghuashun(cardData)
    if data and self:getVal(data[1]) ~= 14 then
        data = {}
    end
    return data
end


function M:searchSameValue(cardData, count)
    local data = {}
    local temp = {}
    local iVal, jVal = 0, 0 
    for i = 1, #cardData do
        temp = {}
        iVal = self:getVal(cardData[i])
        table.insert(temp, cardData[i])
        for j = i + 1, #cardData do
            jVal = self:getVal(cardData[j])
            if jVal == iVal then
                table.insert(temp, cardData[j])
            end
        end
        if #temp == count then
            data = temp
            break
        end
    end
    return data
end

function M:sortByValues(cardData)
    table.sort(cardData, function(a, b)
        return (self:getVal(a) - self:getVal(b)) > 0
    end)
    --[[
    local temp = {}
    for k, v in ipairs(cardData) do
        table.insert(temp, Pcard:getVal(v))
        --table.insert(temp, Pcard:getColor(v))
    end
    ss.traceback.dump(temp)
    ]]
    return cardData
end


return M